//注册模块
define('reg', ['config', 'jquery', 'cookie'], function(config, $, cookie){
	return function(){
		$('#regModal .main-submit').on('click', function(){

			var phone = $('#regPhone').val();
			var code = $('#regCode').val();		//验证码
			var password = $('#regPassword').val();
			var password2 = $('#regPassword2').val();
			// var isTeacher = $('#regModal input[name="regIsTeacher"]:checked').val();
			var flag = true;

			if (phone == ''){
				$('#regModal .error:eq(0)').html('<span>电话不能为空</span>');
				flag = false;
			}
			if (password == ''){
				$('#regModal .error:eq(2)').html('<span>密码不能为空</span>');
				flag = false;
			}
			if (password2 == ''){
				$('#regModal .error:eq(3)').html('<span>密码不能为空</span>');
				flag = false;
			}
			else if (password != password2){
				$('#regModal .error:eq(3)').html('<span>两次输入密码不同</span>');
				flag = false;
			}

			if (flag){	//信息格式正确，向服务器发送登录请求
				var request = $.ajax({
					url : config['api']['reg'][0],
					type : config['api']['reg'][1],
					data : {
						isTeacher : 0,	//只能在b端更改
						phone: phone,
						password: password
					},
					dataType : 'json',
					async : true,
					error : function(err){
						console.log(err);

						if (err['status'] == '403'){
							$('#regModal .error:eq(0)').html('<span>账户已存在</span>');
						}
						else{
							$('#regModal .error:eq(3)').html('<span>连接服务器出错！</span>');
						}

						$('#regPassword').val('');
						$('#regPassword2').val('');
						setTimeout(function(){
							$(document).one('click', function(){
								$('#regModal .error').html('');
							});
						}, 10);
					},
					success : function(data){
						alert('注册成功！');
						//跳转到首页
						window.location.reload();
					}
				});
			}
			else{	//信息格式错误，绑定全局点击事件，去除错误提示信息
				setTimeout(function(){
					$(document).one('click', function(){
						$('#regModal .error').html('');
					});
				}, 10);
			}
		});
	};		
});
